import { resolve } from "path";
import { defineConfig, loadEnv, PluginOption } from "vite";
import vue from "@vitejs/plugin-vue";
import eslintPlugin from "vite-plugin-eslint";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

// https://vite.dev/config/
export default defineConfig(({ command, mode }) => {
	const root = process.cwd();
	const env = loadEnv(mode, root);
	const {
		VITE_APP_API_URL_PROXY,
		VITE_APP_API_URL_PREFIX,
		VITE_APP_BASE_URL,
		VITE_APP_PORT,
		VITE_APP_OPEN,
		VITE_OUT_DIR,
		VITE_DROP_CONSOLE,
		VITE_DROP_DEBUGGER,
		VITE_SOURCEMAP,
	} = env;
	const isBuild = command === "build";
	// vite 插件
	const vitePlugins: (PluginOption | PluginOption[])[] = [
		vue(),
		// 使用 svg 图标
		createSvgIconsPlugin({
			iconDirs: [resolve(__dirname, "./src/assets/iconsvg")],
			symbolId: "icon-[name]",
		}),
		eslintPlugin(),
	];
	// 自动导入
	vitePlugins.push(
		AutoImport({
			imports: [
				"vue",
				"vue-router",
				"pinia",
				{
					"vue-i18n": ["createI18n", "useI18n"],
				},
			],
			resolvers: [ElementPlusResolver()],
			dts: "./src/@types/auto-imports.d.ts",
		}),
	);
	// 组件自动注册
	vitePlugins.push(
		Components({
			dirs: [],
			extensions: ["vue"],
			resolvers: [ElementPlusResolver()],
			dts: "./src/@types/components.d.ts",
		}),
	);

	// proxy
	const proxy = {};
	if (!isBuild) {
		// 不是生产环境
		if (VITE_APP_API_URL_PROXY && VITE_APP_API_URL_PREFIX !== "") {
			proxy[VITE_APP_API_URL_PREFIX] = {
				target: VITE_APP_API_URL_PROXY,
				rewrite: (path) => path.replace(VITE_APP_API_URL_PREFIX, VITE_APP_API_URL_PREFIX),
				changeOrigin: true,
			};
		}
	}
	return {
		root,
		base: VITE_APP_BASE_URL + "/",
		server: {
			host: "0.0.0.0",
			port: Number(VITE_APP_PORT || 3001),
			open: VITE_APP_OPEN === "true",
			cors: true,
			proxy,
		},
		plugins: vitePlugins,
		resolve: {
			extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".scss", ".css"],
			alias: [
				{
					find: /^~/,
					replacement: `${resolve(__dirname, "./node_modules")}/`,
				},
				{
					find: /@\//,
					replacement: `${resolve(__dirname, "./src")}/`,
				},
			],
		},
		css: {
			preprocessorOptions: {
				scss: {
					api: "modern-compiler",
					//   additionalData: '@import "@/assets/scss/index.scss";'
				},
			},
		},
		esbuild: {
			pure: VITE_DROP_CONSOLE === "true" ? ["console.log"] : undefined,
			drop: VITE_DROP_DEBUGGER === "true" ? ["debugger"] : undefined,
		},
		build: {
			rollupOptions: {
				outDir: VITE_OUT_DIR || "dist",
				sourcemap: VITE_SOURCEMAP === "true",
				output: {
					chunkFileNames: "static/js/[name]-[hash].js",
					entryFileNames: "static/js/[name]-[hash].js",
					assetFileNames: "static/[ext]/[name]-[hash].[ext]",
					manualChunks: {
						"vue-chunks": ["vue", "vue-router", "pinia"],
					},
				},
			},
			chunkSizeWarningLimit: 1500,
			minify: "terser",
			terserOptions: {
				compress: {
					drop_console: VITE_DROP_CONSOLE === "true" ? true : false,
					drop_debugger: VITE_DROP_DEBUGGER === "true" ? true : false,
				},
			},
		},
	};
});